import { addOrUpdateAreaAPI, deleteAreaAPI, fetchAreaListAPI, fetchDropListAPI } from '@/api/car/areaApi'

const actions = {
  /* 发送请求获取区域列表的action */
  async fetchAreaList(context, pageParams) {
    const { data } = await fetchAreaListAPI(pageParams)
    context.commit('updateAreaList', data)
  },
  /* 发送请求删除区域的action */
  async deleteArea(context, id) {
    await deleteAreaAPI(id)
    return '删除区域成功'
  },
  /* 发送请求添加获取编辑区域的action */
  async addOrUpdateArea(context, data) {
    await addOrUpdateAreaAPI(data)
    return `${data.id?'编辑区域成功':'添加区域成功'}`
  },
  /* 发送请求获取计费规则的action */
  async fetchDropList(context) {
    const { data } = await fetchDropListAPI()
    context.commit('updateDropList', data)
  }
}
const mutations = {
  /* 更新区域列表的mutation */
  updateAreaList(state, areaListInfo) {
    state.areaListInfo = areaListInfo
  },
  /* 更新关联区域列表的mutation */
  updateDropList(state, dropList) {
    state.dropList = dropList
  }
}
const state = () => {
  return {
    areaListInfo: {},
    dropList: []
  }
}

const getters = {
  areaList: state => state.areaListInfo.rows,
  total: state => state.areaListInfo.total
}

export default {
  actions,
  mutations,
  state,
  getters,
  namespaced: true
}
